package com.lagou.rpc.provider;

import com.lagou.rpc.provider.server.RpcServer;
import com.lagou.rpc.provider.util.ZkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.InetAddress;
import java.util.Properties;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {

    @Autowired
    RpcServer rpcServer;

    public static void main(String[] args) {
        SpringApplication.run(ServerBootstrapApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Properties properties = new Properties();
        properties.load(ServerBootstrapApplication.class.getClassLoader().getResourceAsStream("application.properties"));
        //获取服务所在port
        String port=properties.getProperty("server.port");
        System.out.println("获取服务所在port--->"+port);
        //获取服务所在ip
        String ip= InetAddress.getLocalHost().getHostAddress();
        System.out.println("获取服务所在ip--->"+ip);
        new Thread(() -> rpcServer.startServer(ip, Integer.parseInt(port))).start();
        ZkUtil.saveInfo(ip,port);
    }
}
